<?php

namespace app\models;

use yii\db\ActiveRecord;
use Yii;

class User extends ActiveRecord{
	
	
	public static function tableName(){
		// return "shop_admin";
		return "{{%User}}";
	}
	
	//uId 用户序号	uName 用户昵称	uMail 用户邮箱	uNumber 用户账号	uPassword 密码	uHeadurl 头像 state

	public function rules()
    {
        return [
            [['uPassword'], 'required'],
            [['uName'], 'string', 'max' => 250],
            [['uNumber'], 'string', 'min' => 11, 'max' => 11],
		];
    }

    public function attributeLabels()
    {
        return [
            'uId' => '用户id',
            'uName' => '用户昵称',
            'uEmail' => '用户邮箱',
            'uNumber' => '用户账号',
            'uPassword' => '密码',
            'uHeadurl' => '头像',
            'state' => '状态码',
        ];
    }
    
    //插入数据
    public function insertUser($data){
        //load是php框架内部自主加载数据的函数，而validate是自主验证rules
        $this->uName = $data['uName'];
        $this->uEmail = $data['uEmail'];
        $this->uNumber = $data['uNumber'];
        $this->uPassword = $data['uPassword'];
        //save是php yii内的sql插入函数
        if($this->save()){
            return true;
        } 
		return false;
    }

    //检查邮箱是否重复使用
    public function checkBymail($uEmail){
        $data = self::find()->select('uId')->where('uEmail = :uEmail and state = :state',[':uEmail'=>$uEmail,':state'=>'1'])->asArray()->one();
        if(!empty($data)){
            return true;
        }
        return false;
    }
    //检查手机号码是否重复使用
    public function checkBynumber($uNumber){
        $data = self::find()->select('uId')->where('uNumber = :uNumber and state = :state',[':uNumber'=>$uNumber,':state'=>'1'])->asArray()->one();
        if(!empty($data)){
            return true;
        }
        return false;
    }

    //查找数据
    public function getByuId($uId){
        $data = self::find()->select(['uId','uPassword','uName','uHeadurl'])->where('uId = :uId and state = :state',[':uId'=>$uId,':state'=>'1'])->asArray()->one();
        return $data;
    }

    public function getPasByuId($uId){
        $data = self::find()->select(['uId','uPassword'])->where('uId = :uId and state = :state',[':uId'=>$uId,':state'=>'1'])->asArray()->one();
        return $data;
    }

    public function getAll($number){
        $data = self::find()->select(['uId','uPassword','uName','uEmail','uNumber','uHeadurl'])->where('uNumber = :uNumber and state = :state',[':uNumber'=>$number,':state'=>'1'])->asArray()->one();
        return $data;
    }

    public function getNameByuId($uId){
        $data = self::find()->select(['uId','uName'])->where('uId = :uId and state = :state',[':uId'=>$uId,':state'=>'1'])->asArray()->one();
        return $data;
    }

    public function getByNumber($number){
    	$data = self::find()->select(['uId','uPassword','uName','uHeadurl'])->where('uNumber = :uNumber and state = :state',[':uNumber'=>$number,':state'=>'1'])->asArray()->one();
		return $data;
    }

    public function getByMail($mail){
    	$data = self::find()->select(['uId','uPassword','uName','uHeadurl'])->where('uEmail = :uEmail and state = :state',[':uEmail'=>$mail,':state'=>'1'])->asArray()->one();
		return $data;
    }

    public function checkSameuName($uName){
        $data = self::find()->select(['uId'])->where('uName = :uName and state = :state',[':uName'=>$uName,':state'=>'1'])->asArray()->one();
        return $data;
    }
    
    //修改数据
    public function updatePassword($data){
        $uPassword = $data['uPassword'];
        $key = [
            'uId' => $data['uId'],
        ];
        $changedata = [
            'uPassword' => $uPassword,
        ];
        $result = User::updateAll($changedata,$key);
        if( $result == 1 ){
            return true;
        }
        return false;
    }

    public function updateName($data){
        $uName = $data['uName'];
        $key = [
            'uId' => $data['uId'],
        ];
        $changedata = [
            'uName' => $uName,
        ];
        $result = User::updateAll($changedata,$key);
        if( $result == 1 ){
            return true;
        }
        return false;
    }
}
